/**
 * Created with IntelliJ IDEA.
 * User: Lorenzo
 * Date: 15-7-14
 * Time: 下午11:07
 * To change this template use File | Settings | File Templates.
 */

function initDict() {
    var data = getDictPage(0);
    if(data.status==1) {
        renderDictList(data.data);
    }else {
        alert(data.data);
    }
}

function getDictPage(pageNum) {
    if(pageNum == 0) {
        pageNum = 1;
    }
    var type = $('#type').val();
    var status = $('#status').val();
    var searchData = {
        code : '',
        name : '',
        status : status,
        type : type
    };
    var data = {
        pageSize : pageSize,
        pageNum : pageNum,
        data : "{status:'"+status+"',type:'"+type+"'}"
    }
    return syncPostRequest("/cm/dict/searchpagedict", data);
}

function editDictModal(id) {
    var data = syncGetRequest('/cm/dict/searchdict/'+id);
    if(data.status==1) {
        var dictData = data.data;
        $('#edit_id').val(dictData.id);
        $('#edit_code').val(dictData.code);
        $('#edit_name').val(dictData.name);
        $('#edit_type').val(dictData.type);
        $('#edit_status').val(dictData.status);
        $('#edit_note').val(dictData.note);
        $('#modal_title').html('修改参数字典');
        if($('#editDict').hasClass('save')) {
            $('#editDict').removeClass('save');
        }
        $('#editDict').modal('show');
    }else {
        alert(data.data);
    }
}

function addDictModal() {
    $('.modal_title').html('添加参数字典');
    $('#edit_id').val('');
    $('#edit_code').val('');
    $('#edit_name').val('');
    $('#edit_type').val('');
    $('#edit_status').val('');
    $('#edit_note').val('');
    $('#editDict').addClass('save');
    $('#editDict').modal('show');
}

function deleteDict(id){
    if(!confirm("你确认要删除此条记录吗？")) {
        return false;
    }
    var returnData = syncGetRequest('/cm/dict/deleteDictDef/'+id);
    if(returnData.status==1) {
        initDict();
        $('#errorMessage').html('删除成功');
        $('#errorMessage').removeClass('hide');
        return;
    }
    $('#errorMessage').html('删除失败');
    $('#errorMessage').removeClass('hide');

}

function saveDict() {
    var id = $('#edit_id').val();
    var name = $('#edit_name').val();
    var code = $('#edit_code').val();
    var type = $('#edit_type').val();
    var status = $('#edit_status').val();
    var notes = $('#edit_note').val();
    var postData = {
        id: id,
        code: code,
        name: name,
        type: type,
        status: status,
        note: notes,
        items:[]
    }
    var returnData;
    if($('#editDict').hasClass('save')) {
        returnData = syncPostRequest("/cm/dict/addDictDef", postData);
    }else {
        returnData = syncPostRequest("/cm/dict/updateDictDef", postData);
    }
    if(returnData.status==1) {
        $('#editDict').modal('hide');
        initDict();
        $('#errorMessage').html('保存成功');
        if($('#editDict').hasClass('save')) {
            $('#editDict').removeClass('save');
        }
        $('#errorMessage').removeClass('hide');
        return;
    }
    $('#editDict').modal('hide');
    $('#errorMessage').html('保存失败');
    $('#errorMessage').removeClass('hide');
}

function renderDictList(data) {
    var trStr = "";
    if(data != null && data.length>0) {
        $(data).each(function(i, value){
            trStr += "<tr><td><a href='javascript:;' onclick='dictDetail("+value.id+","+value.code+")'>"+value.code+"</a></td><td>"+value.name+"</td><td>"+value.type
                +"</td><td>"+value.status+"</td><td>"+value.note
                +"</td><td><div class='lineEdit icon-pencil lr5'data-toggle='modal' onclick='editDictModal(\""+value.id+"\")'></div>" +
                "<div class='lineEdit icon-remove lr5' onclick='deleteDict(\""+value.id+"\")'></div></tr>";
        })
    }
    var tabId = $('li.l-selected').attr('tabid');
    $('div[tabid='+tabId+'] .table tbody tr').remove();
    $('div[tabid='+tabId+'] .table tbody').append(trStr);
}

function dictDetail(id, code) {
    var name= "参数明细-"+code;
    var url = "/html/sysmgt/detail.html";
    Global.tabManager.addTabItem({tabid:id, text:name, url:url});
}
initDict();
